package com.yuanrui.auth.domain.service.impl;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.yuanrui.auth.basic.entity.AuthPermission;
import com.yuanrui.auth.basic.entity.AuthRolePermission;
import com.yuanrui.auth.basic.entity.AuthUser;
import com.yuanrui.auth.basic.entity.AuthUserRole;
import com.yuanrui.auth.basic.service.AuthPermissionService;
import com.yuanrui.auth.basic.service.impl.AuthRolePermissionServiceImpl;
import com.yuanrui.auth.basic.service.impl.AuthUserRoleServiceImpl;
import com.yuanrui.auth.basic.service.impl.AuthUserServiceImpl;
import com.yuanrui.auth.common.enums.IsDeletedFlagEnum;
import com.yuanrui.auth.domain.convert.AuthPermissionConverter;
import com.yuanrui.auth.domain.entity.AuthPermissionBO;
import com.yuanrui.auth.domain.redis.RedisUtil;
import com.yuanrui.auth.domain.service.AuthPermissionDomainService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private AuthPermissionService authPermissionService;

    private final String authPermissionPrefix = "auth:permission";
    @Resource
    private AuthRolePermissionServiceImpl authRolePermissionService;
    @Resource
    private AuthUserServiceImpl authUserService;
    @Resource
    private AuthUserRoleServiceImpl authUserRoleService;

    @Override
    public Boolean addPermission(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertToEntity(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int insert = authPermissionService.insert(authPermission);
        return insert > 0;
    }

    @Override
    public Boolean updatePermission(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertToEntity(authPermissionBO);
        int update = authPermissionService.update(authPermission);
        return update > 0;
    }

    /**
     * 删除权限
     *
     * @param authPermissionBO 权限信息
     * @return 是否成功
     */
    @Override
    public Boolean deletePermission(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = new AuthPermission();
        authPermission.setId(authPermissionBO.getId());
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int update = authPermissionService.update(authPermission);
        return update > 0;
    }

    /**
     * 获取权限
     * @param userName 用户名
     * @return 权限列表
     */
    @Override
    public List<String> getPermission(String userName) {
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        String permission = redisUtil.get(permissionKey);
        if (StringUtils.isBlank(permission)){
            return Collections.emptyList();
        }
        List<AuthPermission> permissionList=new Gson().fromJson(permission,
                new TypeToken<List<AuthPermission>>(){
        }.getType());
        return permissionList.stream().map(AuthPermission::getPermissionKey).toList();
    }

    /**
     * 更新用户权限
     * @param userName 用户名
     * @param roleId 角色id
     * @return 是否成功
     */
    @Override
    public Boolean updateUserPermission(String userName, String roleId) {
        //将用户的权限信息存入缓存
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        AuthRolePermission authRolePermission = new AuthRolePermission();
        authRolePermission.setRoleId(Long.valueOf(roleId));
        authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<AuthRolePermission> authRolePermissions = authRolePermissionService.queryByCondition(authRolePermission);
        List<Long> permissionIds = authRolePermissions.stream().map(AuthRolePermission::getPermissionId).toList();
        List<AuthPermission> authPermissions = authPermissionService.queryByIds(permissionIds);
        redisUtil.set(permissionKey, new Gson().toJson(authPermissions));
        //更新用户权限字段
        AuthUserRole authUserRole = new AuthUserRole();
        //根据用户名查询用户id
        AuthUser authUser = new AuthUser();
        authUser.setUserName(userName);
        authUser.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        AuthUser userResult = authUserService.queryUserInfo(authUser);
        authUserRole.setRoleId(Long.valueOf(roleId));
        authUserRole.setUserId(userResult.getId());
        authUserRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int update = authUserRoleService.update(authUserRole);
        return update > 0;
    }

}
